package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsTermInfoMapper;
import cc.rengu.igas.bsps.common.entity.TermInfo;
import cc.rengu.igas.bsps.common.enums.RecordStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/04/16
 */
public class BspsTermInfoMapperImpl implements BspsTermInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<TermInfo> selectBspsTermInfoListByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsTermInfoListByMchntNo");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        termInfo.setTermStatus(RecordStatusEnum.FORMAL.getStatus());
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, termInfo, TermInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号<{}>获取商户终端信息失败", mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (TermInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBspsTermInfoListByMchntNoAndTermNo(String instId, String mchntNo, String termNo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsTermInfoListByMchntNoAndTermNo");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        termInfo.setTermNo(termNo);
        return dbsUtil.dbsFetchPageListData(sqlStr, termInfo, TermInfo.class, pageInfo);
    }

    public TermInfo selectBspsTermInfoByPrimaryKey(String instId, String mchntNo, String termNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsTermInfoByPrimaryKey");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        termInfo.setTermNo(termNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termInfo, termInfo);
        if (0 != iReturnCode) {
            rglog.error("获取终端信息失败,instId:<{}>,mchntNo:<{}>,termNo:<{}>,iReturnCode:<{}>", instId, mchntNo, termInfo, iReturnCode);
            return null;
        }
        return termInfo;
    }

    public int updateTermContactNameContactPhoneByPrimaryKey(String instId, String mchntNo, String termNo, String contactName, String contactPhone) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTermContactNameContactPhoneByPrimaryKey");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        termInfo.setTermNo(termNo);
        termInfo.setContactName(contactName);
        termInfo.setContactPhone(contactPhone);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新终端信息失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }
}
